/*
 * Created by JFormDesigner on Tue Jun 20 20:01:55 CST 2023
 */

package fm.ex_warehouse;

import java.awt.event.*;

import dao.Dao_ex_ware;
import dao.Dao_trade;
import dao.Dao_type;
import dao.Dao_warehousing;
import model.Goods;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class UpdateOut extends JInternalFrame {

    String [] columns = {"出库明细编号", "商品编号", "商品名称", "类别", "单价", "数量","码洋","折扣","实洋"};
    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            Goods goods = (Goods) list.get(i);

            results[i][0] = goods.getDetailID();
            results[i][1] = goods.getGoodsID();
            results[i][2] = goods.getGoodsName();
            results[i][3] = goods.getType();
            results[i][4] = goods.getPrice();
            results[i][5] = goods.getNumber();
            results[i][6] = goods.getTotal();
            results[i][7] = goods.getDiscount();
            results[i][8] = goods.getReal();
        }


        return results;
    }
    public UpdateOut() {
        initComponents();

        Vector<String> its = Dao_ex_ware.getOutID();
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
        comboBox1.setModel(model);

        iniData();
    }

    public void iniData() {
        String wareID = (String) comboBox1.getSelectedItem();

        Object [][] results = getFS(Dao_ex_ware.select_goods_3(wareID));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        table.setModel(model_1);

        int setRow = table.getRowCount();
        double goods_reals = 0.0;
        double goods_totals = 0.0;

        for(int i = 0; i < setRow; i++)
        {
            double totals = Double.parseDouble(table.getValueAt(i, 6).toString().trim());
            double reals = Double.parseDouble(table.getValueAt(i, 8).toString().trim());

            goods_reals += reals;
            goods_totals += totals;

        }

        realsTextField.setText(String.valueOf(goods_reals));
        totalsTextField.setText(String.valueOf(goods_totals));
    }

    private void comboBox1(ActionEvent e) {
       iniData();
    }

    private void tableMouseClicked(MouseEvent e) {
        int setRow = table.getSelectedRow();
        if(setRow == -1) return;

        String detailID = table.getValueAt(setRow,0).toString().trim();
        String trade = table.getValueAt(setRow,2).toString().trim();
        String price = table.getValueAt(setRow,4).toString().trim();
        String number = table.getValueAt(setRow,5).toString().trim();

        priceTextField.setText(price);
        tradeTextField.setText(trade);
        detailTextField.setText(detailID);
        numberTextField.setText(number);
    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void delete(ActionEvent e) {
        String detailID = detailTextField.getText().trim();

        int i = Dao_ex_ware.deleteDetail(detailID);
        System.out.println(i);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"出库明细删除成功！");
            iniData();
        }
    }

    private void update(ActionEvent e) {
        String number = numberTextField.getText().trim();
        String detailID = detailTextField.getText().trim();
        String trade = tradeTextField.getText().trim();

        String o_price = String.valueOf(Dao_trade.getTradeName_price(trade) * Double.parseDouble(number));
        String o_realPrice = String.valueOf(Dao_trade.getTradeName_price(trade) * Dao_trade.getTradeName_discount(trade) * Double.parseDouble(number));


        if(Double.parseDouble(number) - Dao_trade.getTradeName_number(tradeTextField.getText().trim()) > Dao_type.getTypeID_min(Dao_trade.getTradeName_typeID(tradeTextField.getText().trim())))
        {
            JOptionPane.showMessageDialog(null,"库存不足！");
            return;
        }

        int i = Dao_ex_ware.updateDetail(detailID, number, o_price, o_realPrice);
        System.out.println(i);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"修改成功！");
            iniData();
        }
    }

    private void deleteAll(ActionEvent e) {
        String ID = String.valueOf(comboBox1.getSelectedItem());

        int setRow = table.getRowCount();
        if(setRow != 0)
        {
            int j = 0;
            int o = 0;
            for(int i = 0; i < setRow; i++)
            {
                String detailID = table.getValueAt(i, 0).toString().trim();

                o = Dao_ex_ware.deleteDetail(detailID);
                j++;
            }
            JOptionPane.showMessageDialog(null,"您已删除" + j + "条出库明细！");
        }

        int i = Dao_ex_ware.delete_out(ID);

        if( i == 1)
        {
            JOptionPane.showMessageDialog(null,"出库单删除成功！");
            Vector<String> its = Dao_ex_ware.getOutID();
            DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
            comboBox1.setModel(model);
            iniData();
        }
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        updateButton = new JButton();
        deleteButton = new JButton();
        cancelButton = new JButton();
        deleteAllButton = new JButton();
        panel2 = new JPanel();
        comboBox1 = new JComboBox();
        panel3 = new JPanel();
        panel4 = new JPanel();
        label1 = new JLabel();
        detailTextField = new JTextField();
        label2 = new JLabel();
        tradeTextField = new JTextField();
        label3 = new JLabel();
        numberTextField = new JTextField();
        label4 = new JLabel();
        priceTextField = new JTextField();
        label5 = new JLabel();
        totalsTextField = new JTextField();
        label6 = new JLabel();
        realsTextField = new JTextField();
        panel5 = new JPanel();
        scrollPane1 = new JScrollPane();
        table = new JTable();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u4fee\u6539\u51fa\u5e93\u5355");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(0, 50));
            panel1.setLayout(null);

            //---- updateButton ----
            updateButton.setText("\u4fee\u6539");
            updateButton.addActionListener(e -> update(e));
            panel1.add(updateButton);
            updateButton.setBounds(141, 8, updateButton.getPreferredSize().width, 29);

            //---- deleteButton ----
            deleteButton.setText("\u5220\u9664");
            deleteButton.addActionListener(e -> delete(e));
            panel1.add(deleteButton);
            deleteButton.setBounds(236, 9, deleteButton.getPreferredSize().width, 29);

            //---- cancelButton ----
            cancelButton.setText("\u8fd4\u56de");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(476, 9, cancelButton.getPreferredSize().width, 29);

            //---- deleteAllButton ----
            deleteAllButton.setText("\u5220\u9664\u51fa\u5e93\u5355");
            deleteAllButton.addActionListener(e -> deleteAll(e));
            panel1.add(deleteAllButton);
            deleteAllButton.setBounds(new Rectangle(new Point(331, 10), deleteAllButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 479, 693, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(0, 60));
            panel2.setBorder(new TitledBorder("\u8bf7\u9009\u62e9\u51fa\u5e93\u5355"));
            panel2.setLayout(null);

            //---- comboBox1 ----
            comboBox1.addActionListener(e -> comboBox1(e));
            panel2.add(comboBox1);
            comboBox1.setBounds(254, 20, 170, 30);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 0, 693, panel2.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setLayout(new BorderLayout());

            //======== panel4 ========
            {
                panel4.setPreferredSize(new Dimension(0, 120));
                panel4.setBorder(new TitledBorder("\u5165\u5e93\u5355\u660e\u7ec6"));
                panel4.setLayout(null);

                //---- label1 ----
                label1.setText("\u660e\u7ec6\u7f16\u53f7\uff1a");
                panel4.add(label1);
                label1.setBounds(44, 32, label1.getPreferredSize().width, 19);

                //---- detailTextField ----
                detailTextField.setEditable(false);
                panel4.add(detailTextField);
                detailTextField.setBounds(103, 28, 110, detailTextField.getPreferredSize().height);

                //---- label2 ----
                label2.setText("\u5546\u54c1\uff1a");
                panel4.add(label2);
                label2.setBounds(270, 33, label2.getPreferredSize().width, 19);

                //---- tradeTextField ----
                tradeTextField.setEditable(false);
                panel4.add(tradeTextField);
                tradeTextField.setBounds(307, 29, 136, tradeTextField.getPreferredSize().height);

                //---- label3 ----
                label3.setText("\u6570\u91cf\uff1a");
                panel4.add(label3);
                label3.setBounds(500, 34, label3.getPreferredSize().width, 21);
                panel4.add(numberTextField);
                numberTextField.setBounds(539, 29, 80, numberTextField.getPreferredSize().height);

                //---- label4 ----
                label4.setText("\u5355\u4ef7\uff1a");
                panel4.add(label4);
                label4.setBounds(68, 78, label4.getPreferredSize().width, 19);

                //---- priceTextField ----
                priceTextField.setEditable(false);
                panel4.add(priceTextField);
                priceTextField.setBounds(103, 75, 75, priceTextField.getPreferredSize().height);

                //---- label5 ----
                label5.setText("\u603b\u7801\u6d0b\uff1a");
                panel4.add(label5);
                label5.setBounds(260, 79, label5.getPreferredSize().width, 19);

                //---- totalsTextField ----
                totalsTextField.setEditable(false);
                panel4.add(totalsTextField);
                totalsTextField.setBounds(310, 73, 80, totalsTextField.getPreferredSize().height);

                //---- label6 ----
                label6.setText("\u603b\u5b9e\u6d0b\uff1a");
                panel4.add(label6);
                label6.setBounds(491, 77, label6.getPreferredSize().width, 19);

                //---- realsTextField ----
                realsTextField.setEditable(false);
                panel4.add(realsTextField);
                realsTextField.setBounds(542, 73, 75, realsTextField.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel4.getComponentCount(); i++) {
                        Rectangle bounds = panel4.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel4.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel4.setMinimumSize(preferredSize);
                    panel4.setPreferredSize(preferredSize);
                }
            }
            panel3.add(panel4, BorderLayout.NORTH);

            //======== panel5 ========
            {
                panel5.setBorder(new TitledBorder("\u5546\u54c1\u660e\u7ec6"));
                panel5.setLayout(null);

                //======== scrollPane1 ========
                {

                    //---- table ----
                    table.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            tableMouseClicked(e);
                        }
                    });
                    scrollPane1.setViewportView(table);
                }
                panel5.add(scrollPane1);
                scrollPane1.setBounds(7, 19, 678, 269);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel5.getComponentCount(); i++) {
                        Rectangle bounds = panel5.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel5.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel5.setMinimumSize(preferredSize);
                    panel5.setPreferredSize(preferredSize);
                }
            }
            panel3.add(panel5, BorderLayout.CENTER);
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 60, 693, 419);

        contentPane.setPreferredSize(new Dimension(705, 560));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton updateButton;
    private JButton deleteButton;
    private JButton cancelButton;
    private JButton deleteAllButton;
    private JPanel panel2;
    private JComboBox comboBox1;
    private JPanel panel3;
    private JPanel panel4;
    private JLabel label1;
    private JTextField detailTextField;
    private JLabel label2;
    private JTextField tradeTextField;
    private JLabel label3;
    private JTextField numberTextField;
    private JLabel label4;
    private JTextField priceTextField;
    private JLabel label5;
    private JTextField totalsTextField;
    private JLabel label6;
    private JTextField realsTextField;
    private JPanel panel5;
    private JScrollPane scrollPane1;
    private JTable table;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
